
package adoc.xml;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author ILikePancakes
 */
public class UnsignedDocument extends Document {
    private Element root;
    private Namespace namespace = Namespace.getNamespace("http://www.archyvai.lt/adoc/2008/metadata/unsignable");

    public UnsignedDocument() {
        super();
        root = new Element("metadata", namespace);
        this.setRootElement(root);
        this.addUseData();
    }

    public UnsignedDocument(File file) throws IOException, JDOMException {
        super(new SAXBuilder().build(file).cloneContent());
        root = this.getRootElement();
    }

    public UnsignedDocument(String filename) throws IOException, JDOMException {
        this(new File(filename));
    }

    public UnsignedDocument(List<String> documentPaths) throws JDOMException, IOException {
        super();
        root = new Element("metadata", namespace);
        this.setRootElement(root);
        ArrayList<UnsignedDocument> documents = new ArrayList();
        for (String path : documentPaths) documents.add(new UnsignedDocument(path));
        for (UnsignedDocument document : documents) root.addContent(document.getRootElement().cloneContent());
    }

    private Element addToMetadata(String elementName) {
        Element element = new Element(elementName, namespace);
        root.addContent(element);
        return element;
    }

    public Element addUseData() {
        Element use = addToMetadata("Use");
        Element environment = new Element("technical_environment", namespace);
        // environment elementai
        Element standard = new Element("standardVersion", namespace);
        Element category = new Element("documentCategory", namespace);
        Element generator = new Element("generator", namespace);
        Element os = new Element("os", namespace);
        // ju reiksmes
        standard.setText("ADOC-V1.0");
        category.setText("CeDOC");
        generator.setText("aSystem v1.0");
        os.setText(System.getProperty("os.name"));
        // viska sudedame
        environment.addContent(standard);
        environment.addContent(category);
        environment.addContent(generator);
        environment.addContent(os);
        use.addContent(environment);
        return use;
    }
    
}
